using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Formboard.Resources;
using System.Threading;

namespace Formboard
{
	public partial class UserControlBoard : UserControl
	{
		int size;
		String Owner;
		String Guest;
		public CalculBoard goban;
		public crossPics[,] arrayPics;
		public EnumCell myColor;
		public EnumCell enemyColor;
		public bool myturn;
		public int lastSeqNumber;
		public Move movetosend;
		public bool stop = false;
		//   public Thread graphicThread;

		public UserControlBoard (Board board, String myscreenname)
		{
			InitializeComponent ();
			//    graphicThread = new Thread(new ThreadStart(update_graphic_board));
			size = 9;       //size of the board
			arrayPics = new crossPics[size, size];
			Owner = board.owner;
			Guest = board.invitee;
			if (board.owner_white) {
				if (Owner == myscreenname) {
					myColor = EnumCell.White;
					enemyColor = EnumCell.Black;
				} else {
					myColor = EnumCell.Black;
					enemyColor = EnumCell.White;
				}
			} else {
				if (Owner == myscreenname) {
					myColor = EnumCell.Black;
					enemyColor = EnumCell.White;
				} else {
					myColor = EnumCell.White;
					enemyColor = EnumCell.Black;
				}
			}

			//crossPics array initialisation 
			for (int i = 0; i < size; i++) {
				for (int j = 0; j < size; j++) {
					crossPics pic = new crossPics (i, j);
					arrayPics [i, j] = pic;
					this.Controls.Add (pic);
				}
			}
			//calculboard creation
			goban = new CalculBoard (size);
			if (board.moves.Count > 0) {
				lastSeqNumber = board.moves.Count ();
				goban.Board_Reconstruction (board.moves);
				update_graphic_board ();
				int remain = 0;
				System.Math.DivRem (board.moves.Count (), 2, out remain);
				if (remain == 0) { //lastSeqNumber is pair, so last move was white
					if (myColor == EnumCell.White) {
						myturn = false;

					} else {
						myturn = true;
					}
				} else {   //last move was black
					if (myColor == EnumCell.White) {
						myturn = true;
					} else {
						myturn = false;

					}
				}
			} else {
				lastSeqNumber = 0;
				if (myColor == EnumCell.Black) {   //new Board and Im black
					myturn = true;
				} else {     //new Board and Im white
					myturn = false;

				}
			}
            
		}

		private void UserControlBoard_Load (object sender, EventArgs e)
		{
            
		}

		public void update_graphic_board ()
		{
            
			for (int i = 0; i < size; i++) {
				for (int j = 0; j < size; j++) {
					arrayPics [i, j].updateCell (goban.getCell (i + 1, j + 1).value);
				}
			}
		}

		public void TellGrandFatherToSendAMove (int row, int column)
		{
			movetosend = new Move (lastSeqNumber, row, column);
			((Form1)this.Parent).requester.playAMoveRequest (movetosend, ((Form1)this.Parent).CurrentBoard);
		}

		public void TellGrandFatherToUpdateHistory (int seq,EnumCell col, int row, int column)
		{
			((Form1)this.Parent).updateHistory (seq, col, row, column);
		}

		private void tableLayoutPanel1_Paint (object sender, PaintEventArgs e)
		{

		}

        

	}
}
